package com.gec.anan.driver.service.impl;

import com.gec.anan.common.constant.RedisConstant;
import com.gec.anan.common.execption.AnanException;
import com.gec.anan.common.result.Result;
import com.gec.anan.common.result.ResultCodeEnum;
import com.gec.anan.dispatch.client.NewOrderFeignClient;
import com.gec.anan.driver.client.DriverInfoFeignClient;
import com.gec.anan.driver.service.DriverService;
import com.gec.anan.map.client.LocationFeignClient;
import com.gec.anan.model.form.driver.DriverFaceModelForm;
import com.gec.anan.model.form.driver.UpdateDriverAuthInfoForm;
import com.gec.anan.model.vo.driver.DriverAuthInfoVo;
import com.gec.anan.model.vo.driver.DriverLoginVo;
import com.gec.anan.order.client.OrderInfoFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LocationFeignClient locationFeignClient;
    @Autowired
    private NewOrderFeignClient newOrderFeignClient;



    @Override
    public String login(String code) {
//        Result<Long> login = client.Login(code);
//        Long driverId = login.getData();
//        String token = UUID.randomUUID().toString().replace("-", "");
//        redisTemplate.opsForValue().set(token, driverId);
//        return token;


        //1.远程调用返回{code,data：{driverId},message}
        Result<Long> login = driverInfoFeignClient.Login(code);
        //处理调用失败产生的异常
        if(login.getCode()!=200){
            throw new AnanException(login.getCode(),login.getMessage() );
        }
        Long driverId = login.getData();
        //处理远程调用的错误
        if(driverId==null){
            throw  new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        //2.生成token
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //3.将生成的token设置到redis中
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token, driverId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        return token;
    }


    //获取司机登录信息
    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
//        return client.getDriverLoginInfo(driverId).getData();

        Result<DriverLoginVo> driverLoginInfo = driverInfoFeignClient.getDriverLoginInfo(driverId);
        if(driverLoginInfo==null){
            throw new AnanException(driverLoginInfo.getCode(), driverLoginInfo.getMessage());
        }
        DriverLoginVo driverLoginVo = driverLoginInfo.getData();
        if(driverLoginVo==null){
            throw new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        return driverLoginVo;
    }

    //获取司机的认证信息
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> driverAuthInfo = driverInfoFeignClient.getDriverAuthInfo(driverId);
        if(driverAuthInfo==null){
            throw new AnanException(driverAuthInfo.getCode(), driverAuthInfo.getMessage());
        }
//        if(driverAuthInfo.getData()){
//            throw new AnanException(ResultCodeEnum.)
//        }
        return driverAuthInfo.getData();
    }

    //更新司机的认证信息
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        return driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm).getData();
    }

    //创建司机人脸模型
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);
        return booleanResult.getData();
    }

    //判断当日是否进行了人脸识别
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        Result<Boolean> faceRecognition = driverInfoFeignClient.isFaceRecognition(driverId);
        Boolean data = faceRecognition.getData();
        return data;
    }

    //人脸识别
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.verifyDriverFace(driverFaceModelForm);
        Boolean data = booleanResult.getData();
        return data;
    }

    //司机开始接单
    @Override
    public Boolean startService(Long driverId) {
        //根据司机的id查询driverInfo
        //1.判断司机有无认证
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();

        if(driverLoginVo.getAuthStatus().intValue() != 2){
            //未审核
            throw new AnanException(ResultCodeEnum.AUTH_ERROR );
        }
        System.out.println("startService-1");

        //2.判断司机当日进行过人脸识别
        Result<Boolean> faceRecognition = driverInfoFeignClient.isFaceRecognition(driverId);
        Boolean isAuth = faceRecognition.getData();
        if(!isAuth){
            //未人脸识别
            throw new AnanException(ResultCodeEnum.FACE_ERROR );
        }
        //3.接单，修改状态
        //
        driverInfoFeignClient.updateServiceStatus(driverId, 1);

        //5.删除redis中司机的geo位置信息，删除订单队列
        //删除位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清除订单队列信息
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

    //司机停止接单
    @Override
    public Boolean stopService(Long driverId) {
        //更新接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 0);//0表示停止接单
        //删除位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清空订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return null;
    }


}
